package com.zhongkai.web.control.book;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.book.TDmCcsDjzt;
import com.zhongkai.model.book.TDmCcsZxyy;
import com.zhongkai.service.CommonService;
import com.zhongkai.service.book.CancelService;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.DateManageTool;


@Controller
public class CarCancellationControl extends ParamType{
	@Resource
	private CancelService cancelService;
	/**
	 * @param ccdjh接受车船登记的id
	 * @param tDmCcsZxyy：接受cczx_form页面的注销原因
	 * @param zxRq_or_zxhfRq:接受cczx_form页面的注销日期或者恢复日期，注意日期不能为空，否则会抛出异常和错误
	 * @param bz：接受cczx_form页面的备注
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/system/book/car_cancel.do")
	public void Cancellation(Integer ccdjh,TDmCcsZxyy tDmCcsZxyy,Date zxRq_or_zxhfRq,String bz,HttpServletResponse response,HttpServletRequest request) throws Exception{		
		PrintWriter out=response.getWriter();		
		TDjCcdjxx old_tDjCcdjxx=(TDjCcdjxx) cancelService.findById(new TDjCcdjxx(), ccdjh);
		
		/*查找车船登记状态名称*/
		String djztDm=old_tDjCcdjxx.getDjztDm();
		
		/*注销原因名称*/
		String zxyyMc=tDmCcsZxyy.getZxyyMc();		
		if("恢复".equals(zxyyMc)){
			//(1)判断登记状态："09"-------->已注销
			if("09".equals(djztDm)){//98注销恢复的业务处理		
				if(cancelService.cczxAndcchf_Transaction(old_tDjCcdjxx, zxRq_or_zxhfRq,"98",new String[]{bz},request)){
					out.write(CheckWord.showMsg("恭喜你，你的车又可以使用了!", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
				}else{
					out.write(CheckWord.showMsg("系统正在维护，不便之处，请原谅!", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
				}				
			}else{
				out.write(CheckWord.showMsg("本车记录并没有注销过,不能恢复!", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
			}				
		}else{						
			String sbqsny=old_tDjCcdjxx.getSbqsny();//201008
			boolean isfinish=cancelService.isfinish(sbqsny,zxRq_or_zxhfRq);
				if(!isfinish){
					out.write(CheckWord.showMsg("你还没有完税，请完税后再注销！", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
				}else{					
					//登记状态：01--------->正常使用
					if(!"01".equals(djztDm)){
						out.write(CheckWord.showMsg("非正常使用状态，不能修改！", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
					}else{//98注销的业务处理
						if(cancelService.cczxAndcchf_Transaction(old_tDjCcdjxx,zxRq_or_zxhfRq,"99",new String[]{tDmCcsZxyy.getZxyyDm()},request)){
							int[] canceldate=DateManageTool.cutDate(zxRq_or_zxhfRq);//注销日期
							int[] printDate=DateManageTool.cutDate(new Date());//打印日期
							out.write(CheckWord.showMsg("注销成功 ，返回主页面", "/system/book/car_cancelReceipt_form.jsp?zxyy="+tDmCcsZxyy.getZxyyDm()+"&cancelYear="+canceldate[0]+"&cancelMonth="+canceldate[1]+"&cancelDay="+canceldate[2]+"&printYear="+printDate[0]+"&printMonth="+printDate[1]+"&printDay="+printDate[2]+"&ccdjh="+ccdjh,request));
						}else{
							out.write(CheckWord.showMsg("系统正在维护，不便之处，请原谅!", "/system/book/car_cancel_form.jsp?ccdjh="+ccdjh,request));
						}
				}
				
			}
						
		}
		
		
	}


	
	

}
